package com.example.chengche_lol.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.chengche_lol.R;
import com.example.chengche_lol.beans.MenuBean;

import java.util.ArrayList;
import java.util.List;

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MViewHolder> {

    private List<MenuBean> mData;

    public MenuAdapter() {
        mData = new ArrayList<>();
    }

    @NonNull
    @Override
    public MenuAdapter.MViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View rootview = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.item_menu,parent,false);
        return new MViewHolder(rootview);
    }

    @Override
    public void onBindViewHolder(@NonNull MViewHolder holder, int position) {
        MenuBean bean = mData.get(position);
        holder.icon.setImageResource(bean.getIcon());
        holder.title.setText(bean.getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.onClick(position);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(longClickListener!=null){
                    longClickListener.onClick(position);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void addData(MenuBean bean){
        mData.add(bean);
        notifyItemInserted(mData.size());
    }

    public void addData(List<MenuBean> data){
        int lastsize = mData.size();
        mData.addAll(data);
        notifyItemRangeInserted(lastsize,data.size());
    }

    public static class MViewHolder extends RecyclerView.ViewHolder{
        private ImageView icon;
        private TextView title;

        public MViewHolder(@NonNull View itemView){
            super(itemView);
            icon = itemView.findViewById(R.id.menu_icon);
            title = itemView.findViewById(R.id.menu_title);
        }
    }

    //定义接口
    public interface OnItemClickListener{
        void onClick(int position);
    }

    private OnItemClickListener listener;

    //写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemLongClickListener{
        void onClick(int position);
    }

    private OnItemLongClickListener longClickListener;

    public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

}
